new Vue({
    el: '#app',
    data() {
        return {
            user: {},
            roles: [],
            searchUserName: '',
            searchRoleId: 0,
            saveUser: {
                userCode: '',
                userName: '',
                userPassword: '',
                gender: 1,
                birthday: '',
                phone: '',
                address: '',
                userRole: 1
            },
            lastPage: 0,
            showUser: {},
            success: true, // 后端是否正确返回数据
            errorMsg: '' // 后端返回的错误信息
        }
    },
    created() {
        let page = sessionStorage.getItem('nowPage');
        if (page === null || page === undefined) {
            page = 1;
        }
        this.loadUserData(page)
        this.loadAllRole()
        this.loadDataByUserId()
    },
    methods: {
        loadUserData(page) {
            let _this = this;
            axios.get('http://localhost:8011/user/' + page)
                .then((response) => {
                    if (response.data.success) {
                        _this.user = response.data.data;
                        _this.lastPage = response.data.data.pages;
                        // 记录当前页数
                        sessionStorage.setItem('nowPage', page);
                    }
                    _this.success = response.data.success;
                    if (!_this.success) {
                        _this.errorMsg = response.data.data;
                    }
                })
        },
        loadAllRole() {
            let _this = this;
            axios.get('http://localhost:8041/role')
                .then((response) => {
                    _this.roles = response.data.data;
                })
        },
        searchUser() {
            let _this = this;
            axios.get('http://localhost:8011/user/' + _this.searchUserName + "/" + _this.searchRoleId + '/1')
                .then((response) => {
                    _this.user = response.data.data;
                })
        },
        save() {
            let _this = this;
            axios.get('http://localhost:8011/user/'
                + _this.saveUser.userCode
                + '/' + _this.saveUser.userName
                + '/' + _this.saveUser.userPassword
                + '/' + _this.saveUser.gender
                + '/' + _this.saveUser.birthday
                + '/' + _this.saveUser.phone
                + '/' + _this.saveUser.address
                + '/' + _this.saveUser.userRole
                , {
                    params: {
                        'lastPage': _this.lastPage
                    }
                }).then((response) => {
                location.href = 'user.html';
            })
        },
        loadDataByUserId() {
            let uid = sessionStorage.getItem('uid')
            console.log('进入了查看用户信息的方法，id:' + uid)
            if (uid !== null) {
                let _this = this;
                axios.get('http://localhost:8011/user/id/' + uid)
                    .then((response) => {
                        _this.showUser = response.data.data;
                    })
            }
        },
        locationUser(uid, href) {
            sessionStorage.setItem('uid', uid);
            location.href = href;
        },
        removeUser(uid) {
            if (confirm('你确定要删除么？')) {
                let _this = this;
                axios.delete('http://localhost:8011/user/' + uid + '/' + _this.user.pageNum + '/' + _this.user.pages)
                    .then(() => {
                        _this.loadUserData(_this.user.pageNum);
                    })
            }
        },
        updateUser() {
            let _this = this;
            axios.put('http://localhost:8011/user/'
                + _this.showUser.id
                + '/' + _this.showUser.userName
                + '/' + _this.showUser.gender
                + '/' + _this.showUser.birthday
                + '/' + _this.showUser.phone
                + '/' + _this.showUser.address
                + '/' + _this.showUser.role.data.id
                + '/' + sessionStorage.getItem('nowPage')
            ).then(() => {
                //_this.loadUserData(_this.user.pageNum);
                location.href = 'user.html';
            })
        }
    }
})